﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using Telerik.Reporting.Processing;
using Telerik.Reporting.XmlSerialization;

namespace ezCloud.Api.Common
{
    public class Reports
    {
        public static async void ExportNightAuditReport(int hotelId, DateTime date)
        {
            var xmlReader = new ReportXmlSerializer();
            Telerik.Reporting.Report reportToExport = (Telerik.Reporting.Report)xmlReader.Deserialize(AppDomain.CurrentDomain.BaseDirectory + "Reports\\NightAudit.trdx" );
            //passing hidden parameters if needed
            reportToExport.ReportParameters["HotelId"].Value = hotelId;
            reportToExport.ReportParameters["SiteUrl"].Value = WebConfigurationManager.AppSettings["RootUrl"];

            ReportProcessor reportProcessor = new ReportProcessor();
            Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource();
            instanceReportSource.ReportDocument = reportToExport;
            RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null);
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "NightAudits\\" + hotelId + "\\"))
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "NightAudits\\" + hotelId + "\\");
            string fileName = AppDomain.CurrentDomain.BaseDirectory + "NightAudits\\" + hotelId + "\\" + result.DocumentName + "." + result.Extension;
            File.WriteAllBytes(fileName, result.DocumentBytes);
            return;
        }
    }
}